//
//  BaseroutAPI.m
//
//

#import "BaseroutAPI.h"
#import "AppDelegate.h"

#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJSON.h"
#import "MBProgressHUD.h"

@implementation BaseroutAPI

static BaseroutAPI *_sharedInstance = nil;

#pragma mark SINGLETON

+ (BaseroutAPI*)sharedInstance
{
    if (!_sharedInstance) {
        _sharedInstance = [[BaseroutAPI alloc] init]; 
    }
    
    return _sharedInstance;
}

- (id)init {
	if(self = [super init]){
		AppDelegate *appdel = [[UIApplication sharedApplication] delegate];
		configdata = [appdel configData];
	}
	return self;
}

#pragma mark functions yyyyyyyyyyy

- (BOOL) validateString:(NSString*) value
{
    if( value == NULL )
        return NO;
    
    if( value == nil )
        return NO;
    
    if( [value length] == 0 )
        return NO;
    
    if( [value caseInsensitiveCompare:@"null"] == NSOrderedSame)
        return NO;
  
    if( [value caseInsensitiveCompare:@"<null>"] == NSOrderedSame)
        return NO;
    
    if( [value caseInsensitiveCompare:@"(null)"] == NSOrderedSame)
        return NO;
       
    return YES;
       
}

- (void) zSetObj:(NSString *)key theValue:(NSObject*)value
{
    if( value == NULL )
        return;
    
    if( value == nil )
        return;
    
    AppDelegate *appdel = [[UIApplication sharedApplication] delegate];
    configdata = [appdel configData];
    
    [configdata setObject:value forKey:key];
    [appdel writeApplicationPList];
}

- (NSObject *) zGetObj:(NSString *)key;
{
    AppDelegate *appdel = [[UIApplication sharedApplication] delegate];
    configdata = [appdel configData];
    
    return  [configdata valueForKey:key];
}

- (void) zSetVar:(NSString *)key theValue:(NSString*)value
{
    if( value == NULL )
        return;
    
    if( value == nil )
        return;
    
    AppDelegate *appdel = [[UIApplication sharedApplication] delegate];
    configdata = [appdel configData];
    
    [configdata setObject:value forKey:key];
    [appdel writeApplicationPList];
}

- (NSString *) zGetVar:(NSString *)key;
{
    AppDelegate *appdel = [[UIApplication sharedApplication] delegate];
    configdata = [appdel configData];
    
    return  [configdata valueForKey:key];
}

- (NSString *) zGetVarEx:(NSString *)key;
{
    AppDelegate *appdel = [[UIApplication sharedApplication] delegate];
    configdata = [appdel configData];
    
    NSString* ret =  [configdata valueForKey:key];
    if( ret == NULL)
        return @"";
    
    return ret;
}

- (void) MessageBox : (NSString*) title Message:(NSString*) message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

- (BOOL) eMailValidation:(NSString*) Email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:Email];
}

- (NSString*) GetCurrentDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
   return [formatter stringFromDate:[NSDate date]];
}
//
//- (void) zSendServer:(NSMutableDictionary *)slist theView:(UIViewController *)viewcontroller theTitle:(NSString*) title theCommand:(NSString *)command
//{
//    NSString *parmlist = @"";
//    NSArray *listkeys = [slist allKeys];
//    for (NSString *key in listkeys) {
//        NSString *listvalue = [slist objectForKey:key];
//        if( [parmlist isEqualToString:@""] )
//        {
//            parmlist = [NSString stringWithFormat:@"%@=%@", key, listvalue];
//        }
//        else
//        {
//            parmlist = [NSString stringWithFormat:@"%@&%@=%@", parmlist, key, listvalue];
//        }
//    }
//    
//    NSString *theip = @"24.212.212.173:9999";
//    NSString *serverip = [[BaseroutAPI sharedInstance] zGetVarEx:@"ServerIP"];
//    if( [[BaseroutAPI sharedInstance] validateString:serverip] ) {
//        theip = serverip;
//    }
//    
//    NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"http://%@/bb-0.1.0.BUILD-SNAPSHOT/mobile%@?%@", theip, command, parmlist] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//    
//    //NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"http://192.168.1.192:8080/bb-0.1.0.BUILD-SNAPSHOT/mobile%@?%@", command, parmlist] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//    
//    NSLog(@"Request=%@", url);   
//    request = [ASIFormDataRequest requestWithURL:url];
//    [request setRequestMethod:@"GET"]; 
//    [request addRequestHeader:@"Content-type" value:@"application/json"]; 
//    [request setDelegate:viewcontroller];
//    [request startAsynchronous];
//    
//    NSLog(@"showHUD");
//    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:viewcontroller.view animated:YES];
//    hud.labelText = title;
//}
//
//- (void) zStopHttpRequest
//{
//    if( request != nil )
//    {
//        NSLog(@"call clearDelegatesAndCancel");
//        [request clearDelegatesAndCancel];
//    }
//    else
//    {
//        NSLog(@"do not call clearDelegatesAndCancel as it is nil");
//    }
//}

@end
